package neu.edu.cn.nuemall.controller;

import neu.edu.cn.nuemall.entity.Userinfo;
import neu.edu.cn.nuemall.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Autowired
    private UserinfoService userinfoService;


    @RequestMapping("/admin/login")
    public String toAdminLogin(){
        return "/admin/login";
    }

    @RequestMapping("/admin/handler/login")
    public String loginAdmin(Userinfo userinfo, HttpSession session, Model model){
        List<Userinfo> userinfoList = userinfoService.getUserList(userinfo);
        if (!CollectionUtils.isEmpty(userinfoList)) {
            session.setAttribute("USER_INFO", userinfoList.get(0));
            return "forward:/admin/index";
        }
        model.addAttribute("msg","账号或密码错误，请重新输入");
        //返回到登录页面
        return "forward:/admin/login";

    }
}
